home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WWTCLKit / WWTCLInterpIBInspector.m < prev    next >
Encoding:
Text File  |  1995-03-22  |  9.7 KB  |  358 lines

  1.  
  2. #import "WWTCLInterp.h"
  3. #import "WWTCLInterpIBInspector.h"
  4. #import "NIBNameCell.h"
  5. #import "NIBNameMatrix.h"
  6. #import "NIBNameScrollView.h"
  7.  
  8. #import <strings.h>
  9. #import <ctype.h>
  10.  
  11.  
  12. @implementation WWTCLInterpIBInspector
  13.  
  14. /*
  15. ==========================================================================
  16. subext() -- Remove extension from fname only if it is there.
  17.  
  18. ==========================================================================
  19. */
  20. static char 
  21. *subext(fname, ext)
  22. register char *fname, *ext;
  23. {
  24.   int fl, el;
  25.  
  26.     for ( fl = strlen(fname), el = strlen(ext);
  27.         --el > -1 && --fl > -1 && fname[fl] == ext[el]; )
  28.         ;
  29.     if (el == -1) 
  30.     {  fname[fl] = '\0';
  31.     }
  32.     return(fname);
  33. }
  34.  
  35.  
  36. /*
  37. ==========================================================================
  38. basename() -- deletes any prefix ending in `/' and the suffix.
  39.  
  40. ==========================================================================
  41. */
  42. static char 
  43. *basename(str, sfx, dest)
  44. char *str, *sfx, *dest;
  45. {
  46. char temp[1024];
  47. char *p, *p1;
  48. char *subext();
  49.  
  50.     p = p1 = temp;
  51.     strcpy(p, str);
  52.     while (*p) 
  53.     {  if (*p++ == '/') 
  54.            {  p1 = p;
  55.        }
  56.     }
  57.     strcpy (dest, subext(p1, sfx));
  58.     return(dest);
  59. }
  60.  
  61. - init
  62. {
  63.    id      bundle, retId;
  64.    char    path[MAXPATHLEN + 1];
  65.    NXRect  scrollRect, matrixRect;
  66.    NXSize  interCellSpacing = {0.0, 0.0}, cellSize;
  67.    int     i, howManyArchives;
  68.    char    **argv;
  69.    id      newCell;
  70.  
  71.     
  72.    [super init];
  73.  
  74.    bundle = [NXBundle bundleForClass: [WWTCLInterp class]];
  75.  
  76.    if (!bundle) 
  77.    {  NXLogError("No bundle for Class %s.\n", [WWTCLInterp name]);
  78.       return nil;
  79.    }
  80.             
  81.    if (![bundle getPath: path forResource: "WWTCLInterpIBInspector" ofType: "nib"])
  82.    {  NXLogError("No path for %s.\n", "WWTCLInterpIBInspector.nib");
  83.       return nil;
  84.    }
  85.             
  86.    if (![NXApp loadNibFile:path owner:self  withNames:NO fromZone:[self zone]])
  87.    {  NXLogError("problem loading %s.\n");
  88.       return nil;
  89.    }
  90.   /* set the scrollView's attributes */
  91.   [nibNameScrollView setBorderType:NX_BEZEL];
  92.   [nibNameScrollView setVertScrollerRequired:YES];
  93.   [nibNameScrollView setHorizScrollerRequired:YES];
  94.   [nibNameScrollView setAutoresizeSubviews:YES];
  95.         
  96.   /* get the scrollView's dimensions */
  97.     [nibNameScrollView getFrame:&scrollRect];
  98.  
  99.   /* determine the matrix bounds */
  100.     [ScrollView getContentSize:&(matrixRect.size)
  101.             forFrameSize:&(scrollRect.size)
  102.         horizScroller:YES
  103.         vertScroller:YES
  104.         borderType:NX_BEZEL];
  105.     
  106.   /* prepare a matrix to go inside our scrollView */
  107.     matrixRect.origin.x = matrixRect.origin.y = 0.0;
  108.     nibNameMatrix = [[NIBNameMatrix alloc] initFrame:&matrixRect 
  109.                                                       mode:NX_LISTMODE
  110.                                                       cellClass:[TextFieldCell class]
  111.                                                       numRows:0
  112.                                                       numCols:1];
  113.  
  114.   /* we don't want any space between the matrix's cells  */
  115.     [nibNameMatrix setIntercell:&interCellSpacing];
  116.       
  117.   /* resize the matrix's cells and size the matrix to contain them */
  118.     [nibNameMatrix getCellSize:&cellSize];
  119.     cellSize.width = NX_WIDTH(&matrixRect);
  120.     [nibNameMatrix setCellSize:&cellSize];
  121.     [nibNameMatrix sizeToCells];
  122.     [nibNameMatrix setAutosizeCells:YES];
  123.     
  124.   /*
  125.    * when the user clicks in the matrix and then drags the mouse out of
  126.    * scrollView's contentView, we want the matrix to scroll
  127.    */
  128.     [nibNameMatrix setAutoscroll:YES];
  129.     [nibNameMatrix setScrollable:YES];
  130.     
  131.     /* make it so the archive knows about the inspector; this is so when something gets cut we stay in synch. */
  132.     [nibNameMatrix setTheInspector:self];
  133.  
  134.   /* stick the matrix in our scrollView */
  135.     retId = [nibNameScrollView setMyMatrix:nibNameMatrix];
  136.     retId = [nibNameScrollView setDocView:nibNameMatrix];
  137.     retId = [nibNameScrollView docView];
  138.  
  139.   /* set things up so that the matrix will resize properly */
  140.     [[nibNameMatrix superview] setAutoresizeSubviews:YES];
  141.     [nibNameMatrix setAutosizing:NX_WIDTHSIZABLE];
  142.     
  143.   /* set the matrix's single- and double-click actions */
  144.     [nibNameMatrix setTarget:self];
  145.     [nibNameMatrix setAction:@selector(singleClick:)];
  146.     [nibNameMatrix setDoubleAction:@selector(doubleClick:)];
  147.     
  148.    howManyArchives = [object nibNameArgc];
  149.    argv = [object nibNameArgv];
  150.  
  151.    for (i = 0; i < howManyArchives; i++)
  152.    {  [nibNameMatrix addRow];
  153.       [nibNameMatrix sizeToCells];
  154.       newCell = [nibNameMatrix cellAt:i :0];
  155.       [newCell setStringValue:argv[i]];
  156.    }
  157.  
  158.   /* select a cell, for starters */
  159.    [[nibNameMatrix selectCellAt:0 :0] sendAction];
  160.  
  161.    [self revert: nil];
  162.  
  163.    return self;
  164. }
  165.  
  166.  
  167. - singleClick:sender
  168. {  //fprintf(stderr, "been single-clicked\n"); return self;
  169.    return self;
  170. }
  171.  
  172.  
  173. - doubleClick:sender
  174. {  
  175.   char        filename[MAXPATHLEN+1];
  176.   const char  *path;
  177.   id          aCell, 
  178.               aList = nil;
  179.   int         row, col, i;
  180.  
  181.  
  182.   //fprintf(stderr, "been double-clicked\n");
  183.   path = [object pathInIB];
  184.   if (!path)
  185.   {  NXRunAlertPanel("Yo!", 
  186.                  "you better set the pathInIB if you want me to be able to open a nib file when you double-click...", 
  187.                      NULL, NULL, NULL);
  188.      return self;
  189.   }
  190.  
  191.   aList = [[List alloc] init];
  192.   [nibNameMatrix getSelectedCells:aList];
  193.   if ([aList count])
  194.   {  for (i = 0; i < [aList count]; i++)
  195.      {  aCell = [aList objectAt:i];
  196.         [nibNameMatrix getRow:&row andCol:&col ofCell:aCell];
  197.         sprintf(filename, "%s/%s.nib", path, [aCell stringValue]);
  198.         //NXLogError("asking the Workspace to open nib file %s...\n", filename);
  199.         [[Application workspace] openFile:filename];
  200.      }
  201.   }
  202.   else
  203.   {  aCell = [nibNameMatrix selectedCell];
  204.      [nibNameMatrix getRow:&row andCol:&col ofCell:aCell];
  205.      sprintf(filename, "%s/%s.nib", path, [aCell stringValue]);
  206.      //NXLogError("asking the Workspace to open nib file %s...\n", filename);
  207.      [[Application workspace] openFile:filename];
  208.   }
  209.   [aList free];
  210.   
  211.   return self;
  212. }
  213.  
  214.  
  215. - (BOOL)wantsButtons
  216. {
  217.         return YES;
  218. }
  219.  
  220.  
  221. //  Must always call [super revert:].
  222. - revert:sender
  223. {
  224.    int     i, howManyArchives;
  225.    char    **argv;
  226.    id      newCell;
  227.  
  228.  
  229.    [nibNameText setStringValue:""];
  230.  
  231.    // empty out the NibName matrix in preparation for refilling it.
  232.    howManyArchives = [[nibNameMatrix cellList] count];
  233.    for (i = 0; i < howManyArchives; i++)
  234.    {  [nibNameMatrix removeRowAt:0 andFree:YES];
  235.       [nibNameMatrix sizeToCells];
  236.    }
  237.    howManyArchives = [object nibNameArgc];
  238.    argv = [object nibNameArgv];
  239.    for (i = 0; i < howManyArchives; i++)
  240.    {  [nibNameMatrix addRow];
  241.       [nibNameMatrix sizeToCells];
  242.       newCell = [nibNameMatrix cellAt:i :0];
  243.       [newCell setStringValue:argv[i]];
  244.    }
  245.   /* select a cell, for starters */
  246.    [[nibNameMatrix selectCellAt:0 :0] sendAction];
  247.  
  248.    [pathInIBText setStringValue:[object pathInIB]];
  249.  
  250.    return [super revert:sender];
  251. }
  252.  
  253.  
  254. - notok: sender
  255. {
  256.   return [super ok:sender];
  257. }
  258.  
  259. - ok: sender
  260. {
  261.    int   i, realCnt;
  262.    char  *cellStr;
  263.    char  **argv;
  264.  
  265.  
  266.   //  Must always call [super ok:].
  267.  
  268.   argv = (char **)calloc(64, sizeof(char *));
  269.  
  270.   [self addNibName:[nibNameText stringValue]];
  271.  
  272.    // need to grab text out of nibNameText.  If not empty, alloc a new Cell for the nibMatrix 
  273.    // free the old version
  274.    realCnt = 0;
  275.    for (i = 0; i < [[nibNameMatrix cellList] count]; i++)
  276.    {  if ([[[nibNameMatrix cellList] objectAt:i] stringValue])
  277.       {  realCnt++;
  278.       }
  279.    }
  280.    realCnt = 0;
  281.    for (i = 0; i < [[nibNameMatrix cellList] count]; i++)
  282.    {  cellStr = (char *)[[[nibNameMatrix cellList] objectAt:i] stringValue];
  283.       if (cellStr != NULL)
  284.       {  argv[i] = NXCopyStringBuffer(cellStr);
  285.          realCnt++;
  286.       }
  287.    }
  288.    [object setNibNameArgc:realCnt Argv:argv];
  289.    for (i = 0; i < realCnt; i++)
  290.    {  free(argv[i]);
  291.    }
  292.    free(argv);
  293.  
  294.    // set the value back to an empty string
  295.    [nibNameText setStringValue:(const char *)""];
  296.    [nibNameMatrix display];
  297.  
  298.    [object setPathInIB:[pathInIBText stringValue]];
  299.  
  300.    return [super ok: sender];
  301. }
  302.  
  303. - setPathInIBText:sender { pathInIBText = sender; return self; }
  304.  
  305. - addNibName:(const char *)nibFileName
  306. {
  307.   BOOL isWhiteSpace = YES, alreadyGotIt;
  308.   int  i, len;
  309.   char tmpBuf[MAXPATHLEN + 1], *cellStr, *suffix;
  310.  
  311.  
  312.   if (!nibFileName)
  313.   {  return self;
  314.   }
  315.   i = 0; len = strlen(nibFileName);
  316.   while ((i < len) && isWhiteSpace)
  317.   {  if (!isspace(nibFileName[i++]))
  318.      {  isWhiteSpace = NO;
  319.      }
  320.   }
  321.  
  322.   suffix = (char *)nibFileName + (strlen(nibFileName) - 4);
  323.   if ((!isWhiteSpace) && !strcmp(".nib", suffix))
  324.   {  // at this point, we know this is a nib file (or at least named one) and we want to add it to our list.
  325.      // we don't, however, really want to add *this* file.  We want it to be in the project that this nib file is in.  
  326.      // so, for the list, we really want to strip the pathname off the front and the .nib extension at the end.
  327.      // we also want to message Project Builder to add this file to the project.
  328.      // Actually, we also want to make sure that we don't already have this file in our list already.
  329.      alreadyGotIt = NO;
  330.      basename(nibFileName, ".nib", tmpBuf);
  331.      for (i = 0; i < [[nibNameMatrix cellList] count]; i++)
  332.      {  cellStr = (char *)[[[nibNameMatrix cellList] objectAt:i] stringValue];
  333.         if (cellStr)
  334.         {  if (!strcmp(cellStr, tmpBuf))
  335.            {  alreadyGotIt = YES;
  336.            }
  337.         }
  338.      }
  339.      if (!alreadyGotIt)
  340.      {  [nibNameMatrix addRow];
  341.         [nibNameMatrix sizeToCells];
  342.         [[nibNameMatrix cellAt:([nibNameMatrix cellCount] - 1) :0] setStringValue:tmpBuf];
  343.         [nibNameMatrix scrollCellToVisible:([nibNameMatrix cellCount] - 1) :0];
  344.      }
  345.      else
  346.      {  //NXLogError("<%s> already in the matrix.  I did NOT add it\n", tmpBuf);
  347.      }
  348.   }
  349.   else
  350.   {  //NXLogError("<%s> not a nib file.  I did NOT add it\n", nibFileName);
  351.   }
  352.   return self;
  353. }
  354.  
  355.  
  356.  
  357. @end
  358.